package com.egg.ylt.activity.health;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.egg.ylt.R;
import com.egg.ylt.Utils.Constants;
import com.egg.ylt.Utils.CustomUtils;
import com.egg.ylt.Utils.RecyclerViewUtil;
import com.egg.ylt.Utils.StatusBarUtil;
import com.egg.ylt.adapter.ADA_ShopAdapter;
import com.egg.ylt.fragment.FRA_HealthData;
import com.egg.ylt.pojo.NatatoriumListPageEntity;
import com.egg.ylt.pojo.UpdateShopEventBean;
import com.egg.ylt.presenter.impl.UpdateShopImpl;
import com.egg.ylt.view.IUpdateShopView;
import com.yonyou.framework.library.base.BaseActivity;
import com.yonyou.framework.library.bean.ErrorBean;
import com.yonyou.framework.library.eventbus.EventCenter;
import com.yonyou.framework.library.widgets.pla.LoadMoreListView;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ACT_UpdateShop extends BaseActivity<UpdateShopImpl> implements IUpdateShopView {

    @BindView(R.id.include_rl_view)
    RelativeLayout mIncludeRlView;
    @BindView(R.id.include_iv_back)
    ImageView includeIvBack;
    @BindView(R.id.include_tv_title)
    TextView includeTvTitle;
    @BindView(R.id.load_list_view)
    RecyclerView loadListView;
    @BindView(R.id.loading_view)
    RelativeLayout loadingView;
    @BindView(R.id.swipe_refresh)
    SwipeRefreshLayout swipeRefresh;

    private String pageSize = "20";
    private int listSize = 0;
    private int pageNum = 1;
    private ADA_ShopAdapter ada_shopAdapter;
    private RecyclerViewUtil mRecyclerViewUtil;

    @Override
    protected void getBundleExtras(Bundle extras) {

    }

    @Override
    protected int getContentViewLayoutID() {
        return R.layout.act_update_shop;
    }

    @Override
    protected View getLoadingTargetView() {
        return loadingView;
    }

    @Override
    protected void initViewsAndEvents() {
        StatusBarUtil.setStatusBarColor(this, R.color.ylt_whit);
        StatusBarUtil.StatusBarLightMode(this);
        CustomUtils.setImmersiveStatusBar(mContext, mIncludeRlView);

        //设置title
        includeTvTitle.setText("切换游泳馆");

        //初始化下拉刷新
        initRefresh();
        //上拉加载
        initLoadList();
        mPresenter.getShopData(Constants.TOKEN, pageSize, String.valueOf(pageNum), Constants.COMPANYID);
    }

    /**
     * 上拉加载
     */
    private void initLoadList() {
        //设置适配器
        loadListView.setLayoutManager(new LinearLayoutManager(mContext));
        ada_shopAdapter = new ADA_ShopAdapter(mContext);
        loadListView.setAdapter(ada_shopAdapter);

        //设置上拉加载更多
        mRecyclerViewUtil = new RecyclerViewUtil(loadListView);
        mRecyclerViewUtil.setRecyclerViewLoadMoreListener(new RecyclerViewUtil.RecyclerViewLoadMoreListener() {
            @Override
            public void onLoadMore() {
                if (listSize == Integer.parseInt(pageSize)) {
                    //请求门店展示数据
                    pageNum++;
                    mPresenter.getShopData(Constants.TOKEN, pageSize, String.valueOf(pageNum), Constants.COMPANYID);
                    mRecyclerViewUtil.setLoadMoreEnable(false);
                }
            }
        });

        //条目点击事件
        ada_shopAdapter.setOnItemClickListener(new ADA_ShopAdapter.OnItemClickListener() {
            @Override
            public void onClick(String shopId, String shopName) {
                UpdateShopEventBean bean = new UpdateShopEventBean();
                bean.setShopId(shopId);
                bean.setShopName(shopName);
                EventBus.getDefault().post(new EventCenter(Constants.UPDATE_SHOP_EVENT, bean));
                finish();
            }
        });

    }

    /**
     * 下拉刷新
     */
    private void initRefresh() {
        //下拉刷新的设置
        swipeRefresh.setColorScheme(R.color.color_FE752D);
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                //请求门店展示数据
                pageNum = 1;
                mPresenter.getShopData(Constants.TOKEN, pageSize, String.valueOf(pageNum), Constants.COMPANYID);
            }
        });
    }

    @Override
    protected boolean isApplyStatusBarTranslucency() {
        return false;
    }

    @Override
    protected boolean isBindEventBusHere() {
        return false;
    }

    @Override
    protected boolean toggleOverridePendingTransition() {
        return false;
    }

    @Override
    protected TransitionMode getOverridePendingTransitionMode() {
        return null;
    }

    @Override
    protected boolean isApplyKitKatTranslucency() {
        return false;
    }

    @Override
    public void showBusinessError(ErrorBean error) {

    }

    @Override
    public void getShopData(NatatoriumListPageEntity entity) {
        if (entity == null) {
            toggleShowEmpty(true, R.drawable.empty_no_response, "暂无游泳馆信息");
            return;
        }
        toggleShowEmpty(false, -1, "");
        listSize = entity.getList().size();
        if (pageNum == 1) {
            swipeRefresh.setRefreshing(false);
            ada_shopAdapter.setDataList(entity.getList());
        } else {
            //加载完数据后回复上拉状态
            mRecyclerViewUtil.setLoadMoreEnable(true);
            ada_shopAdapter.addDataList(entity.getList());
        }

    }


    @OnClick({R.id.include_iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.include_iv_back://返回键
                finish();
                break;
        }
    }
}
